(function (window, $, undefined) {
  'use strict';

  var $tableCity = $('#table-city');
  var $tableAddress = $('#table-address');

  var tableData = {};
  var curCity = {};
  var curAddress = {};

  main(function () {

    util.jqGrid($tableCity, {
      url: ajaxURL.ORGANIZATION + '/city/getCityList',
      datatype: 'json',
      autowidth: true,
      height: $(window).height() - 250,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { label: '序号', name: 'kid', key: true, width: 50, hidden: true },
        { label: '编号', name: 'cityId', fixed: true, width: 100 },
        { label: '城市名称', name: 'name', width: 100 },
        {
          label: '添加时间', name: 'createTime', width: 100, formatter: function (v, o, r) {
            return util.dateFormat(v);
          }
        }
      ],
      pager: '#pager-city'
    });

    util.jqGrid($tableAddress, {
      url: ajaxURL.ORGANIZATION + '/suggestAddress/getSuggestAddressList',
      // datatype: 'json',
      width: 800,
      height: 400,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { label: '序号', name: 'kid', key: true, width: 50, fixed: true, hidden: true },
        { label: '地点名称', name: 'name', width: 300 },
        { label: '经度', name: 'lng', width: 80 },
        { label: '纬度', name: 'lat', width: 80 }/*,
        {
          label: '添加时间', name: 'createTime', width: 120, formatter: function (v, o, r) {
            return util.dateFormat(v);
          }
        },
        {
          label: '修改时间', name: 'updateTime', width: 120, formatter: function (v, o, r) {
            return util.dateFormat(v);
          }
        }*/
      ],
      pager: '#pager-address'
    });

    function searchTableAddressByCityId(cityId) {
      util.searchGrid($tableAddress, {
        cityId: cityId
      });
    }

    var $layerAddress = $('#layer-address');

    $('#btn-setting').click(util.oneCheck($tableCity, function (id, data) {
      searchTableAddressByCityId(data.cityId);
      curCity = data;
      util.layerOpen({
        title: '配置推荐地点，当前城市：' + data.name,
        content: $layerAddress,
        area: ['820px', '']
      });
    }));

    util.dragMap({
      selector: [
        '#btn-add',
        '#btn-edit'
      ],
      prev: [
        null,
        function () {
          var ids = util.getGridSelectedIds($tableAddress);
          var ret = ids.length === 1;
          if (ret) {
            curAddress = $tableAddress.jqGrid('getRowData', ids[0]);
          } else {
            util.tip.warn('请先选择一条数据');
          }
          return ret;
        }
      ],
      dynamic: false,
      init: [
        function (map) {
          // 设置地图中心
          map.setCity(curCity.name);
        },
        function (map) {
          // 设置地图中心
          map.setZoomAndCenter(16, [curAddress.lng, curAddress.lat]);
        }
      ],
      callback: [
        function (data) { console.log(data)
          util.post(ajaxURL.ORGANIZATION + '/suggestAddress/addSuggestAddress', {
            cityId: curCity.cityId,
            lat: data.position.lat,
            lng: data.position.lng,
            name: data.address
          }, function (res) {
            util.tip.success('添加成功');
            $tableAddress.trigger('reloadGrid');
          });
        },
        function (data) { console.log(data)
          util.post(ajaxURL.ORGANIZATION + '/suggestAddress/updateSuggestAddress', {
            kid: +curAddress.kid,
            cityId: curCity.cityId,
            lat: data.position.lat,
            lng: data.position.lng,
            name: data.address
          }, function (res) {
            util.tip.success('修改成功');
            $tableAddress.trigger('reloadGrid');
          });
        }
      ]
    });

    $('#btn-del').click(util.oneCheck($tableAddress, function (id, data) {
      layer.confirm('确定删除该地址？', function () {
        util.post(ajaxURL.ORGANIZATION + '/suggestAddress/removeSuggestAddress?kid=' + id, function (res) {
          util.tip.success('删除成功');
          $tableAddress.trigger('reloadGrid');
        });
      });
    }));


  });


})(window, jQuery);
